package com.sip.stream.utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import kotlin.UByte;

public class ByteUtils {
    public static void main(String[] strArr) {
        System.out.println(genOpenDoorHexString(43707, (byte) 1));
        System.out.println(Arrays.toString(intToBytes(16909060, 8)));
        System.out.println(Arrays.toString(intToBytes(16909060, 8, ByteOrder.LITTLE_ENDIAN)));
        System.out.println(Arrays.toString(longToBytes(16909060L, 10, ByteOrder.BIG_ENDIAN)));
        System.out.println(Arrays.toString(longToBytes(16909060L, 10, ByteOrder.LITTLE_ENDIAN)));
        System.out.println(align("12", 4, true, '0'));
        System.out.println(align("12", 6, false, 'x'));
        System.out.println(1);
        ByteBuffer wrap = ByteBuffer.wrap(new byte[4]);
        wrap.asIntBuffer().put(16909060);
        System.out.println(Arrays.toString(wrap.array()));
        wrap.rewind();
        wrap.order(ByteOrder.LITTLE_ENDIAN);
        wrap.asIntBuffer().put(16909060);
        System.out.println(Arrays.toString(wrap.array()));
        byte[] bArr = {1, 0, 0, 0};
        System.out.println(bytesToInt(bArr, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToInt(bArr, ByteOrder.BIG_ENDIAN));
        byte[] bArr2 = {1, 0, 0, 0, 17};
        System.out.println(bytesToInt(bArr2, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToInt(bArr2, ByteOrder.BIG_ENDIAN));
        byte[] bArr3 = {1, 0, 0};
        System.out.println(bytesToInt(bArr3, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToInt(bArr3, ByteOrder.BIG_ENDIAN));
        byte[] bArr4 = {1, 0};
        System.out.println(bytesToInt(bArr4, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToInt(bArr4, ByteOrder.BIG_ENDIAN));
        byte[] bArr5 = {1};
        System.out.println(bytesToInt(bArr5, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToInt(bArr5, ByteOrder.BIG_ENDIAN));
        byte[] bArr6 = {1, 0, 0, 0, 0, 0, 0, 0};
        System.out.println(bytesToLong(bArr6, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToLong(bArr6, ByteOrder.BIG_ENDIAN));
        byte[] bArr7 = {1, 0, 0, 0, 0, 0, 0, 0, 18};
        System.out.println(bytesToLong(bArr7, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToLong(bArr7, ByteOrder.BIG_ENDIAN));
        byte[] bArr8 = {1, 0, 0, 0, 0};
        System.out.println(bytesToLong(bArr8, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToLong(bArr8, ByteOrder.BIG_ENDIAN));
        byte[] bArr9 = {1, 0, 0};
        System.out.println(bytesToLong(bArr9, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToLong(bArr9, ByteOrder.BIG_ENDIAN));
        byte[] bArr10 = {1};
        System.out.println(bytesToLong(bArr10, ByteOrder.LITTLE_ENDIAN));
        System.out.println(bytesToLong(bArr10, ByteOrder.BIG_ENDIAN));
    }

    public static String genOpenDoorHexString(int i, byte b) {
        return "000000" + byteToHexString(b) + bytesToHexString(intToBytes(i));
    }

    public static int bytesToInt(byte[] bArr, ByteOrder byteOrder) {
        int length = bArr.length;
        if (length < 4) {
            byte[] bArr2 = new byte[4];
            for (int i = 0; i < 4; i++) {
                int i2 = byteOrder == ByteOrder.LITTLE_ENDIAN ? length : 4 - length;
                if (i < i2) {
                    bArr2[i] = byteOrder == ByteOrder.LITTLE_ENDIAN ? bArr[i] : (byte) 0;
                } else {
                    bArr2[i] = byteOrder == ByteOrder.LITTLE_ENDIAN ? (byte) 0 : bArr[i - i2];
                }
            }
            ByteBuffer wrap = ByteBuffer.wrap(bArr2);
            wrap.order(byteOrder);
            return wrap.getInt();
        }
        ByteBuffer wrap2 = ByteBuffer.wrap(bArr);
        wrap2.order(byteOrder);
        return wrap2.getInt();
    }

    public static long bytesToLong(byte[] bArr, ByteOrder byteOrder) {
        int length = bArr.length;
        if (length < 8) {
            byte[] bArr2 = new byte[8];
            for (int i = 0; i < 8; i++) {
                int i2 = byteOrder == ByteOrder.LITTLE_ENDIAN ? length : 8 - length;
                if (i < i2) {
                    bArr2[i] = byteOrder == ByteOrder.LITTLE_ENDIAN ? bArr[i] : (byte) 0;
                } else {
                    bArr2[i] = byteOrder == ByteOrder.LITTLE_ENDIAN ? (byte) 0 : bArr[i - i2];
                }
            }
            ByteBuffer wrap = ByteBuffer.wrap(bArr2);
            wrap.order(byteOrder);
            return wrap.getLong();
        }
        ByteBuffer wrap2 = ByteBuffer.wrap(bArr);
        wrap2.order(byteOrder);
        return wrap2.getLong();
    }

    public static byte[] intToBytes(int i) {
        return intToBytes(i, 4, ByteOrder.BIG_ENDIAN);
    }

    public static byte[] intToBytes(int i, int i2) {
        return intToBytes(i, i2, ByteOrder.BIG_ENDIAN);
    }

    public static byte[] intToBytes(int i, int i2, ByteOrder byteOrder) {
        if (i2 < 4) {
            return null;
        }
        ByteBuffer allocate = ByteBuffer.allocate(i2);
        allocate.order(byteOrder);
        allocate.position(byteOrder == ByteOrder.LITTLE_ENDIAN ? 0 : i2 - 4);
        allocate.asIntBuffer().put(i);
        return allocate.array();
    }

    public static byte[] longToBytes(int i) {
        return longToBytes(i, 8, ByteOrder.BIG_ENDIAN);
    }

    public static byte[] longToBytes(int i, int i2) {
        return longToBytes(i, i2, ByteOrder.BIG_ENDIAN);
    }

    public static byte[] longToBytes(long j, int i, ByteOrder byteOrder) {
        if (i < 8) {
            return null;
        }
        ByteBuffer allocate = ByteBuffer.allocate(i);
        allocate.order(byteOrder);
        allocate.position(byteOrder == ByteOrder.LITTLE_ENDIAN ? 0 : i - 8);
        allocate.asLongBuffer().put(j);
        return allocate.array();
    }

    public static String bytesToHexString(byte[] bArr) {
        StringBuilder sb = new StringBuilder("");
        if (bArr == null || bArr.length <= 0) {
            return null;
        }
        for (byte b : bArr) {
            String hexString = Integer.toHexString(b & UByte.MAX_VALUE);
            if (hexString.length() < 2) {
                sb.append(0);
            }
            sb.append(hexString);
        }
        return sb.toString();
    }

    public static String byteToHexString(byte b) {
        String hexString = Integer.toHexString(b & UByte.MAX_VALUE);
        return hexString.length() < 2 ? "0" + hexString : hexString;
    }


    public static String bytesToAcsiiString(byte[] bArr) {
        if (bArr == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : bArr) {
            sb.append((char) b);
        }
        return sb.toString();
    }

    public static byte[] AcsiiStringToBytes(String str) {
        if (str == null) {
            return null;
        }
        return str.getBytes();
    }

    public static String align(String str, int i, boolean z, char c) {
        return String.format("%" + (z ? "-" : "") + i + "s", str).replace(" ", c + "");
    }
}
